<div class="row animate fadeIn">
    <form class="form-horizontal" id="list-form">
    <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Add</i></h5>
        <div class="ibox-tools">    
             <button class="btn btn-primary btn-sm save-btn-tab" id="submit-uom">Save</button>
             <a id="cancel_add" class="btn btn-sm btn-outline btn-info">Cancel</a>
        </div>
     </div>
     <div class="ibox-content">
        
            <div class="form-group">
                <label class="col-lg-2 control-label">List Name</label>
                <div class="col-lg-5">
                    <input type="text" id="lm" class="form-control" name="list_name">
                </div>
                <input type="hidden" name="company_id" value="1">
             </div>
             <div class="form-group">
                <label class="col-lg-2 control-label">Description</label>
                <div class="col-lg-5">
                    <input type="text" id="uom_c"class="form-control" name="list_desc">
                </div>
            </div>
        </form>
    </div>
    <!-- row for sequence add -->
    <div id="list_sequence">
      <div id="" class="ibox-content">
        <h3 style="color: #00afef">Create Sequence List</h3>
          <form  id="sequence-form" class="form-inline">
              <input type="hidden" id="list_id" name="c_list_id">
                  <table id="pgTable"class="table table-bordered" style="width:80%;">
                    <thead>
                      <tr>
                        <th style="width:100px;">Sequence</th>
                        <th style="width:170px;">Charge</th>
                        <th>Option</th>
                        <th style="width:70px;">Value</th>
                        <th style="width:50px;">Action</th>
                      </tr>
                      <tr>
                        <td style="margin:0px;padding:0px;">
                            <input style="width:100%;text-align: right;" type="text" id="seq_num" name="seq_num" class="form-control-custom">
                        </td>
                        <td style="margin:0px;padding:0px;">
                            <select id="list_chg_code" class="chosen-select" name="list_code">
                                <option value=""> -- </option>
                                <?php foreach($elements as $chg) { ?>
                                    <option value="<?php echo $chg->charge_id; ?>"> <?php echo $chg->charge_name; ?> </option>
                                <?php } ?>
                            </select>
                        </td>
                        <td style="margin:0px;padding:0px;">
                            <select id="list_chg_opt" class="chosen-select" name="list_opt">
                            </select>
                        </td>
                        <td style="margin:0px;padding:0px;">
                            <input style="width:100%;" style="text-align: right;" type="text" id="op_val"  name="opt_val"  class="form-control-custom" readonly>
                        </td>
                        <td style="margin:0px;padding:0px;">
                            <button style="width:100%;" class="btn btn-white" id="gana"><i class="fa fa-plus success"></i></button>
                        </td>
                        
                      </tr> 
                    </thead>
                    </form>
                    <tbody id="tbodyadd" class="animate fadeIn">
                                                      
                    </tbody>
                  </table> 
      </div>
    </div>
    <!-- end for row sequence -->
 </div>
<script>
    $(document).ready(function () {
      $('#list-form').data('serialize',$('#list-form').serialize());
                var mID;
                $(".chosen-select").chosen({width:"100%;"});
                $('#list_sequence').hide();
                var counter = 0;

              $('.i-checks').iCheck({
                    checkboxClass: 'icheckbox_square-green',
                    radioClass: 'iradio_square-green',
              });

              $('#list_chg_code').change(function(){
                    var id = $('#list_chg_code').val();
                    console.log(id);
                    $.ajax({
                    type: "POST",
                    url: "<?php echo base_url($this->session->userdata('forajax').'/search_option'); ?>",
                    data: { "chg_elem_id" : id },
                    dataType: 'json',
                    success: function(e){
                        var counter = 0;
                        var tmp = "";
                        $.each(e, function(i, item) { counter++;
                            tmp += "<option value='"+e[i].option_code+"'>"+e[i].option_name+"</option>";
                        });
                        $('#list_chg_opt').html(tmp).trigger("chosen:updated");
                        if(counter != 0){
                            $('#op_val').val(e[0].option_value);
                        }else{
                            $('#op_val').val('');
                        }
                    }       
                    });
                });
              $('#list_chg_opt').change(function(){
                    var code = $('#list_chg_opt').val();
                    //console.log(id);
                    $.ajax({
                    type: "POST",
                    url: "<?php echo base_url($this->session->userdata('forajax').'/search_opt_val'); ?>",
                    data: { "opt_elem_code" : code },
                    dataType: 'json',
                    success: function(e){
                        var tmp = "";
                        $('#op_val').val(e[0].option_value);
                    }       
                    });
                });


            $("#list-form").validate({
                 rules: {
                     list_name: {
                         required: true,
                         lettersonly: true
                     }
                 }
                 ,
                 submitHandler: function(form){
                    dataString = $("#list-form").serialize();
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/insert_charge_list'); ?>",
                        data: dataString,
                        dataType: 'json',
                    success: function(data){
                           if(data[0].error == 1){
                            notifymsg("danger",data.msg);
                           }else{
                           notifymsg("success",". . .New List Added Successfully");
                           mID = data[0].id; console.log(mID);
                           $('#list_id').val(mID);
                           $('#list_sequence').show();
                           $('#list-form').find('input, button').attr('disabled','disabled');
                           $('#cancel_add').html('Complete');
                              }
                            }      
                        });
                 }
             });

            $.validator.setDefaults({ ignore: ":hidden:not(select)" })
             $("#sequence-form").validate({
                rules:{
                  seq_num: {
                      required: true,
                      number: true
                  },
                  list_code: {
                      required: true
                  },
                  list_opt: {
                      required: true
                  }
                },
                submitHandler: function(form){
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/insert_charge_list_seq'); ?>",
                        data: $('#sequence-form').serialize(),
                        dataType: 'json',
                    success: function(data){
                        console.log(data);
                        if(data[0].error == 1){
                            notifymsg("danger",data[0].msg);
                        }else{
                        $('#tbodyadd').empty();
                         console.log("there should be a notification");
                        var trHTML = '';
                        notifymsg("success",". . .List Added Successfully");
                        $.each(data, function(i, item) {
                            trHTML += '<tr><input id="line_id" type="hidden" value="'+ data[i].line_id +'"><input id="list_id" type="hidden" value="'+ data[i].list_id +'"><td>' + data[i].seq_num + '</td><td>' + data[i].charge_name + '</td><td>' + data[i].option_name + '</td><td>' + data[i].option_value + '</td><td style="margin:0px;padding:0px;"><a style="width:100%;" class="btn btn-outline btn-danger del" id="gana"><i class="fa fa-times success"></i></a></td></tr>';
                        });
                        $('#tbodyadd').append(trHTML);
                        $("#list_chg_code").val("").trigger("chosen:updated");
                        $("#list_chg_opt").empty().trigger("chosen:updated");
                        $('#sequence-form')[0].reset();
                          }
                      }
                    });
                 }
               });

               $(document).on('click','.btn.btn-outline.btn-danger.del',function(){
                    var lineID          = $(this).closest('tr').children('#line_id').val();
                    var listID          = $(this).closest('tr').children('#list_id').val();
                    console.log(listID);
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/delete_charge_list_seq'); ?>",
                        data: { "line-id": lineID , "list-id": listID },
                        dataType: 'json',
                    success: function(data){
                        console.log(data);

                        if(data.length != 0){

                        if(data[0].error == 1){
                            notifymsg("danger",data[0].msg);
                        }else{
                        $('#tbodyadd').empty();
                         console.log("there should be a notification");
                        var trHTML = '';
                        notifymsg("success",". . .List Sequence Delete Successfully");
                        $.each(data, function(i, item) {
                           trHTML += '<tr><input id="line_id" type="hidden" value="'+ data[i].line_id +'"><td>' + data[i].seq_num + '</td><td>' + data[i].charge_name + '</td><td>' + data[i].option_name + '</td><td>' + data[i].option_value + '</td><td style="margin:0px;padding:0px;"><button style="width:100%;" class="btn btn-outline btn-danger del" id="gana"><i class="fa fa-times success"></i></button></td></tr>';
                        });
                        $('#tbodyadd').append(trHTML);
                        }
                      }else{
                          $('#tbodyadd').empty();
                        }
                      }
                    });
                    
                }); 

          
        function alert_cancel(){
            if($('#list-form').serialize()!=$('#list-form').data('serialize')){
                swal({
                    title: "Are you sure?",
                    text: "Your work will not be save",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Leave",
                    cancelButtonText: "Stay",
                    closeOnConfirm: true,
                    closeOnCancel: true },
                function (isConfirm) {
                    if (isConfirm) {
                        close_add(); active_search();
                        notifymsg("hide","hide");
                    } else {
                        swal("Cancelled");
                    }
                });
                }else{
                close_add(); active_search();
                notifymsg("hide","hide");
            }
        } 
        $('#cancel_add').click(function () {
          if($('#cancel_add').html() == "Complete"){
            close_add(); active_search();
            notifymsg("hide","hide");
          }else{            
            alert_cancel();
          }
         });
        $('#addX').click(function(){
            alert_cancel();        
       });
    });
</script>